import java.util.*;

public class javazfx_4 {
    public static void main(String args[]){
        Scanner reader = new Scanner(System.in);
        Stack<Integer> numbers = new Stack<Integer>();
        int i;
        int n, o, o2;
        n = reader.nextInt();
        for(i=0; i<n; i++){
            o = reader.nextInt();
            if(o == 0){
                if(numbers.empty()){
                    System.out.println("invalid");
                }else{
                    System.out.println(numbers.pop());
                }
            }else{
                o2 = reader.nextInt();
                numbers.push(o2);
            }
        }
    }
}
